package com.web_bloks.utilities.client;

import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;

/**
 * Fixes HTML <a> element(s) for IE's shortcoming
 * when it comes to triggering History's 'onHistoryChanged' event.
 * 
 * @author Jean-Lou Dupont
 *
 */
public class AnchorsUtil {

	public static void updateAnchors() {
		
		Document doc = Document.get();
		NodeList<Element> anchors = doc.getElementsByTagName("a");
		
		if (null==anchors) return;
		
		for(int i=0;i<anchors.getLength();i++) {
			Element e = anchors.getItem(i);
			//make sure we are dealing with a local link
			String  href = e.getAttribute( "href" );
			if (href.indexOf('#') != -1)
				fixForHistory(e);
		}
		
	}//
	protected static native void fixForHistory(Element e) /*-{
	
		e.onclick = function() {
			var href = e.href.split('#')[1];
			@com.google.gwt.user.client.History::newItem(Ljava/lang/String;)(href);
		}
		
	}-*/;
	

}//END